package com.ruoyi.app.controller.login;

import com.ruoyi.app.business.login.LoginBusiness;
import com.ruoyi.app.config.annotation.ExcludeLoginVerify;
import com.ruoyi.app.domain.user.LoginUserInfo;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.domain.APIResult;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录相关
 *
 * @author Zane
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/user/login")
public class LoginController {

  @Resource
  private LoginBusiness loginBusiness;


  /**
   * 手机号码登录
   *
   * @param userType  用户类型（UP 平台用户 UD 项目方用户 UL 劳务方用户 ）
   * @param phone     手机号吗,不能为空
   * @param checkCode 验证码,不能为空
   * @return 登录信息
   */
  @ExcludeLoginVerify
  @GetMapping("{userType}/byMobile")
  public APIResult<LoginUserInfo> byMobile(@PathVariable("userType") String userType,
      @RequestParam("phone") String phone,
      @RequestParam("checkCode") String checkCode) {
    LoginUserInfo loginUserInfo = loginBusiness.loginByMobile(phone, checkCode, userType);
    return APIResult.success(loginUserInfo);
  }

  /**
   * 用户名密码登录
   *
   * @param userType 用户类型（UP 平台用户 UD 项目方用户 UL 劳务方用户 ）
   * @param userName 用户名,不能为空
   * @param password 密码,不能为空
   * @return 登录信息
   */
  @ExcludeLoginVerify
  @GetMapping("/{userType}/byName")
  public APIResult<LoginUserInfo> byNamePwd(@PathVariable("userType") String userType,
      @RequestParam("userName") String userName,
      @RequestParam("password") String password) {
    LoginUserInfo loginUserInfo = loginBusiness.loginByNamePwd(userName, password, userType);
    return APIResult.success(loginUserInfo);
  }

  /**
   * 刷新token
   *
   * @param token 旧token
   * @return 新token
   */
  @ExcludeLoginVerify
  @GetMapping("/refreshToken")
  public APIResult<LoginUserInfo> refreshToken(
      @RequestHeader(SecurityConstants.AUTHORIZATION_HEADER) String token) {
    LoginUserInfo loginUserInfo = loginBusiness.refreshToken(token);
    return APIResult.success(loginUserInfo);
  }

  /**
   * 退出(暂时前端实现,清掉Token即可)
   *
   * @return 登出结果
   */
  @GetMapping("/logout")
  public APIResult<Void> logout() {
    return APIResult.success();
  }


}
